
require 'mime/types'

include Bean

module Oos4ruby
class User < Bean::BeanClass
  
  define_el_reader({
    OosNamespace => [:name, :surname, :no_mails, :only_contacts, :no_newsletter],
    AtomNamespace => [:title, :updated, :content]
  })
  
  alias :nick :title
  alias :about_me :content
  
  attr_reader :slug
  
  attr_writer :nick, :about_me, :name, :surname, :no_mails, :only_contacts, :no_newsletter
  
  def initialize(entry, auth, slug = nil)
    @xml = entry
    @auth = auth
    @slug = slug || @xml.child('slug', OosNamespace).text
  end
  
  def User.find( auth, slug = nil )
    raise Oos4ruby::UnknownUser if auth.method?(:app) && slug.nil?
    
    uri = USERS_URL
    if slug
      if slug.is_a?URI
        uri = slug      
      elsif slug.is_a?String and slug =~ /^http/
        uri = URI.parse(slug)
      else
        uri += "/#{slug}" 
      end
    end
    
    getter = HTTPInvoker.new uri.to_s, auth
    
    worked = getter.get
    
    if worked
      if slug #response is an Entry
        entry = Entry.new getter.body
        return User.new(entry, auth, slug)
      else
        #response is a feed
        feed = Feed.read getter.body        
        rel_edit = feed.entries[0].link('edit')
        
        getter = HTTPInvoker.new rel_edit, auth
        worked = getter.get
        if worked
          entry = Entry.new getter.body
          return User.new(entry, auth)
        else
          raise Oos4ruby::UnknownUser
        end
      end
    end    
  end
  
  def to_xml
    @xml.to_s
  end
  
  def sites( opts = {} )
    @sites_collection = Site.find_by_user(@auth, @slug, opts) unless @sites_collection
    @sites_collection
  end
  
  def contacts( opts = {} )
    @contacts_collection = Contact.find_by_user(@auth, @slug, opts) unless @contacts_collection
    @contacts_collection
  end
  
  def avatar
    @xml.link('avatar')
  end
  
  def avatar!(file_path)
    getter = HTTPInvoker.new @xml.link('edit-media'), @auth
    worked = getter.get
    
    if worked
      file = File.new(file_path, 'w+')
      file << getter.body
    end
  end
  
  def update_avatar!(file_path)
    file = File.open(file_path, "rb") { |f| f.read } 
        
    putter = HTTPInvoker.new(@xml.link('edit-media'), @auth)
    
    putter.set_header 'Content-Length', File.size(file_path)
    
    worked = putter.put MIME::Types.type_for(file_path)[0].to_s, file
    
    raise RuntimeError unless worked
  end
  
  private
  def load!    
      @xml.child('title').text = @nick if @nick
      if @about_me
        about = @xml.child('content') || @xml.add_child('content', nil, nil, {:type => 'text'})
        about.text = @about_me
      end
      
      @xml.child('name', OosNamespace).text = @name if @name
      @xml.child('surname', OosNamespace).text = @surname if @surname
      @xml.child('nomails', OosNamespace).text = @no_mails.to_s unless @no_mails.nil?
      @xml.child('nonewsletter', OosNamespace).text = @no_newsletter.to_s unless @no_newsletter.nil?
      @xml.child('onlycontacts', OosNamespace).text = @only_contacts.to_s unless @only_contacts.nil?
  end
end
end



